cmake_minimum_required(VERSION 3.12)
project(format_examples)

# Check if the compiler supports C++20 and C++23
include(CheckCXXCompilerFlag)
if(MSVC)
  check_cxx_compiler_flag("/std:c++20" COMPILER_SUPPORTS_CXX20)
  check_cxx_compiler_flag("/std:c++23" COMPILER_SUPPORTS_CXX23)
  check_cxx_compiler_flag("/std:c++latest" COMPILER_SUPPORTS_CXXLATEST)
else()
  check_cxx_compiler_flag("-std=c++20" COMPILER_SUPPORTS_CXX20)
  check_cxx_compiler_flag("-std=c++23" COMPILER_SUPPORTS_CXX23)
endif()

if(MSVC)
  if(COMPILER_SUPPORTS_CXX23)
    add_compile_options(/std:c++23)
  elseif(COMPILER_SUPPORTS_CXXLATEST)
    add_compile_options(/std:c++latest)
  else()
    set(CMAKE_CXX_STANDARD 20)
  endif()
  add_compile_options(/Zc:__cplusplus /utf-8)
else()
  if(COMPILER_SUPPORTS_CXX23)
    add_compile_options(-std=c++23)
  else()
    set(CMAKE_CXX_STANDARD 20)
  endif()
endif()

find_package(fmt)

include(../libcxxdetect.cmake)

set(STDLIB_SUPPORTS_FMT FALSE)

if(CMAKE_CXX_COMPILER_ID MATCHES GNU AND
   CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 13.0)
  set(STDLIB_SUPPORTS_FMT TRUE)
elseif(CMAKE_CXX_COMPILER_ID MATCHES Clang)
  detect_libcxx()
  if(LIBCXX_FOUND AND LIBCXX_VERSION VERSION_GREATER_EQUAL 17)
    set(STDLIB_SUPPORTS_FMT TRUE)
  endif()
elseif(MSVC AND MSVC_VERSION GREATER_EQUAL 1929)
  set(STDLIB_SUPPORTS_FMT TRUE)
endif()

if(NOT STDLIB_SUPPORTS_FMT AND NOT (fmt_FOUND AND fmt_VERSION VERSION_GREATER_EQUAL 8.0))
  message(WARNING "The environment is too old for format examples. Project is skipped.")
  return()
endif()

add_executable(fmt_test01_basic_format     test01_basic_format.cpp)
add_executable(fmt_test02_chrono_format    test02_chrono_format.cpp)
add_executable(fmt_test03_ranges_format    test03_ranges_format.cpp)
add_executable(fmt_test04_custom_formatter test04_custom_formatter.cpp)
add_executable(fmt_test05_print            test05_print.cpp)
add_executable(fmt_test06_print_utf8       test06_print_utf8.cpp)
add_executable(fmt_test07_format_locale    test07_format_locale.cpp)

if(fmt_FOUND AND fmt_VERSION VERSION_GREATER_EQUAL 8.0)
  # The fmt library might be needed in all test examples.
  target_link_libraries(fmt_test01_basic_format     fmt::fmt)
  target_link_libraries(fmt_test02_chrono_format    fmt::fmt)
  target_link_libraries(fmt_test03_ranges_format    fmt::fmt)
  target_link_libraries(fmt_test04_custom_formatter fmt::fmt)
  target_link_libraries(fmt_test05_print            fmt::fmt)
  target_link_libraries(fmt_test06_print_utf8       fmt::fmt)
  target_link_libraries(fmt_test07_format_locale    fmt::fmt)
endif()
